/*
Content: Master file
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


clear
clear mata 
clear matrix
set matsize 2000
capture log close 
set more off       // Assures the program not to 'BREAK'
set maxvar 8000

********************************************************************************
**# DEFINE PATHS
********************************************************************************

global path "\\Iab.baintern.de\dfs\017\Ablagen\D01700-Projekte\4525-HomeEvents"
global data "$path/Daten"

global dataieb "$data/iebdata"
	global orig "$dataieb/orig"
	global intermediate "$dataieb/intermediate"
	global final "$dataieb/final"
	global help "$dataieb/help"
	
global dataextra "$data/extradatasets"

global dataevent "$data/eventsdata"
	global uppsala "$dataevent/uppsala"
	global globalterror "$dataevent/globalterror"
	
global codes "$data/country_codes"


global prog "$path/prog/replication_jole/IEB"
	 global prep "$prog/help/prep"
global table "$path/tabs/replication_jole/IEB"
global graph "$path/figs/replication_jole/IEB"
global log "$path/log"


********************************************************************************
*** ADOS
********************************************************************************

adopath ++ "N:\Ablagen\D01700-BQE\ado\"
adopath ++ "N:\Ablagen\D01700-BQE\ado"


sysdir set SITE "N:\Ablagen\D01700-Allgemein\STATA\bocode/"	


********************************************************************************
*** DATA REQUIREMENTS
********************************************************************************

	/* 
		
		LABOR MARKET OUTCOMES:
		
			* Integrated Employment Biographies (IEB):
				- Starting point is 40% random sample of individuals with at least 1 "nationality" spell as non-German
				- Version: v14.00 (up to 2018)
				- The dataset can only be accessed at the Institute for Employment Research, upon approval of a project proposal.
				
			* Establishment Panel (BHP):
				- version: v7522_v1
				
			* Establishment-level AKM coefficients: 
				- version: akm_bet_8521_v1
		
			* Plant closure data: 
				- As prepared through the routine by Schmieder et al. 2023 using BHP
				- Establishment-level bankruptcy (version: bhp_7520_m06_iv_v1)
		
		*******************************************
		
		EVENTS:
		
			* GLOBAL TERROR DATABASE: 
				- for the paper we used the version from 1970-2017, downloaded November 19, 2020
				
			* UPPSALA CONFLICT DATA: 
				- UCDP Georeferenced Event Dataset, downloaded May 8, 2024	
		
		*******************************************

		Supporting datasets: 
			
		 * Correspondence table between country codes in event datasets and IEB country identifiers: 
			- countrycodes_ieb_armedconflict.dta
			- countrycodes_ieb_terror.dta
			
		 * Correspondence between SOEP and IEB country of origin identifiers
			- country_codes_soep_ieb.xlsx
		
		*/ 
		
********************************************************************************
*** PREPARE EVENT DATASETS
********************************************************************************

	**# TERRORIST ATTACKS

	do "$prog/events_prep/1_1_IEB_event_prep.do"		// Basic preparation of event data
	do  "$prog/events_prep/1_2_IEB_create_monthly_events.do"  // Definition of relevant and isolated events + pre-post setup
	do  "$prog/events_prep/1_3_IEB_create_daily_attacks.do" // Daily terrorist attacks and rolling sums
	do  "$prog/events_prep/1_4_IEB_create_extradates_final.do" // Alternative definitions for relevant and isolated events for robustness checks 

	**# ARMED CONFLICTS 
	
	do "$prog/events_prep/1_5_IEB_create_monthly_ArmedConflict_final.do"  // Definition of relevant and isolated events + pre-post setup with Uppsala Conflict Data


********************************************************************************
**# PREPARE IEB DATA FOR THE ANALYSIS
********************************************************************************

	do "1_IEB_basic_dataprep_final.do"		// Runs basic routines to clean and prepare the Integrated Employment Biographies (IEB)
	do "2_IEB_create_collapsed_data_final.do" // Extracts information on adjacent spells and restricts to employment spells only
	do "3_IEB_events_variables_final.do"	// Links with event data + firm-level characteristics and main outcomes

********************************************************************************
**# ANALYSIS
********************************************************************************

	do "4_IEB_mainregressions_final.do" 			// Tables 6,7,10 
	do "5_IEB_balance_final.do"						// Table G.1
	do "6_IEB_robustness_final.do"					// Figures G.2, G.4, Tables G.2, G.3
	do "7_IEB_placebo_final.do"						// Figure G.3
	do "8_IEB_masslayoffs_final.do"					// Table G.5
	do "9_IEB_alttreatment_final.do"				// Table G.5
	do "10_IEB_armedconflict_final.do"				// Table G.6
	do "11_IEB_balance_3months_final.do"			// Table G.7
	do "12_IEB_actualreturns_final.do"				// Table G.8
	do "13_IEB_daily_attacks_final.do"				// Table G.4
	do "14_IEB_occupational_downgrading_final.do"	// Table G.1
	
	
********************************************************************************
**# HELP DO-FILE
********************************************************************************
	
	**# IEB PREPARATION ROUTINES	
	
		* folder "prog/help/prep"
	
		// Note: this runs within "1_basic_dataprep.do"
	
	**# PLACEBOS
	
		*do "$prog/events_prep/2_IEB_create_PLACEBO_monthly_events_final.do"
	
		// Note: this runs within "7_IEB_placebo_final.do"

		
	**# OCCUPATIONAL SCORES
	
		*do "$help/99_IEB_create_occscores.do"
	
		// Note: this runs before 14_*
	
